package com.yami.shop.rights.common.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.Product;
import com.yami.shop.bean.vo.ProdDetailVo;
import com.yami.shop.bean.vo.ProdRightsVo;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.mystery.common.model.MysteryBox;
import com.yami.shop.mystery.common.service.MysteryBoxService;
import com.yami.shop.rights.common.dao.ProdRightsKtMapper;
import com.yami.shop.rights.common.dao.ProdRightsMapper;
import com.yami.shop.rights.common.dto.ProdRightsDto;
import com.yami.shop.rights.common.model.ProdRights;
import com.yami.shop.rights.common.model.ProdRightsKt;
import com.yami.shop.rights.common.service.ProdRightsService;
import com.yami.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProdRightsServiceImpl extends ServiceImpl<ProdRightsMapper, ProdRights> implements ProdRightsService {

    @Autowired
    private ProdRightsMapper prodRightsMapper;
    @Autowired
    private ProdRightsKtMapper prodRightsKtMapper;
    @Autowired
    private ProductService productService;
    @Autowired
    private MysteryBoxService mysteryBoxService;


    @Override
    public IPage<ProdRights> pageList(PageParam<ProdRights> page, ProdRightsDto dto) {
        QueryWrapper<ProdRights> prodRightsQueryWrapper = new QueryWrapper<>();
        prodRightsQueryWrapper.eq("create_at", dto.getShopId());
        prodRightsQueryWrapper.like(ObjectUtil.isNotEmpty(dto.getRightsName()), "rights_name", dto.getRightsName());
        prodRightsQueryWrapper.eq(ObjectUtil.isNotEmpty(dto.getType()), "type", dto.getType());
        prodRightsQueryWrapper.orderByDesc("create_time");
        // -1 为删除权益状态
        prodRightsQueryWrapper.ne("status", -1);
        return prodRightsMapper.selectPage(page, prodRightsQueryWrapper);
    }

    @Override
    public List<ProdRightsVo> getList(ProdRights dto) {
        QueryWrapper<ProdRights> prodRightsQueryWrapper = new QueryWrapper<>();
        prodRightsQueryWrapper.eq("create_at", dto.getCreateAt());
        prodRightsQueryWrapper.like(ObjectUtil.isNotEmpty(dto.getRightsName()), "rights_name", dto.getRightsName());
        prodRightsQueryWrapper.orderByDesc("create_time");
        // -1 为删除权益状态
        prodRightsQueryWrapper.notIn("status", -1,3);
        List<ProdRights> prodRights = prodRightsMapper.selectList(prodRightsQueryWrapper);
        return BeanUtil.copyToList(prodRights, ProdRightsVo.class);
    }

    @Override
    public List<ProdDetailVo> getProdDetail(String rights) {
        List<ProdDetailVo> prodDetailVos = new ArrayList<>();
        List<ProdRightsKt> prodRightsKts = prodRightsKtMapper.selectList(new QueryWrapper<ProdRightsKt>().eq("rights_id", rights));
        prodRightsKts.forEach(s->{
            ProdDetailVo prodDetailVo = new ProdDetailVo();
            if(s.getBlindType().equals("1")){
                Product product = productService.getById(s.getBlindId());
                prodDetailVo.setProdName(product.getProdName());
                prodDetailVo.setPic(product.getPic());
                prodDetailVo.setBlindType("1");
                prodDetailVo.setProdId(product.getProdId());
            }else {
                MysteryBox mysteryBox = mysteryBoxService.getById(s.getBlindId());
                prodDetailVo.setProdName(mysteryBox.getProdName());
                prodDetailVo.setPic(mysteryBox.getPic());
                prodDetailVo.setBlindType("2");
                prodDetailVo.setProdId(mysteryBox.getId());
            }
            prodDetailVos.add(prodDetailVo);
        });
        return prodDetailVos;
    }

}
